﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;

namespace Ersur
{
    public partial class ProductReport : XtraReport
    {
        DataSet ds;
        int EmptyLabelCount;
        public bool Stop = false;
        public ProductReport()
        {
            InitializeComponent();
        }

        public ProductReport(DataSet dsSource, int emptyLabelCount, int copyCount)
            : this()
        {
            EmptyLabelCount = emptyLabelCount;

            DataSet dsOrj = dsSource.Copy();

            ds = dsSource;


            for (int i = 0; i < copyCount - 1; i++)
            {
                ds.Merge(dsOrj);
            }

            for (int i = 0; i < EmptyLabelCount; i++)
            {
                DataRow r = ds.Tables["Table"].NewRow();
                ds.Tables["Table"].Rows.InsertAt(r, 0);
            }

            if (ds.Tables["Table"].Columns["Sira"] != null)
            {
                DataView dv = ds.Tables["Table"].DefaultView;
                dv.Sort = "Sira";
                DataTable sortedDT = dv.ToTable();
                sortedDT.TableName = "Table";
                ds.Tables.Remove("Table");
                ds.Tables.Add(sortedDT);
            }

            this.DataMember = "Table";
            this.DataSource = ds;

            tcArticle.DataBindings.Add("Text", null, "Table.Article");
            tcCount.DataBindings.Add("Text", null, "Table.Count");
            tcKompozisyon.DataBindings.Add("Text", null, "Table.Composition");
            tcMin.DataBindings.Add("Text", null, "Table.MinDYELot");

            xrBarCode1.DataBindings.Add("Text", null, "Table.ProductId");
            lblBarcode.DataBindings.Add("Text", null, "Table.ProductId");
        }       
        
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            EmptyLabelCount--;
        }
        
        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 12f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;

            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }    
        
        private void tcKompozisyon_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }        

        private void xrBarCode1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void xrTable2_BeforePrint_1(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void tbKumas_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void xrPictureBox1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void tcArticle_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcDesign_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcWeight_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcWidth_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }
 
    }
}
